package com.sdp.lease.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.lease.db.l_tenant_info.entity.LTenantInfo;
import com.sdp.lease.db.l_tenant_info.service.ILTenantInfoService;
import com.sdp.lease.utils.LeaseConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-25
 */
@RestController
@RequestMapping("/LTenantInfoController")
public class LTenantInfoController {

	@Autowired
	private Query query;
	
	@Autowired
	private ILTenantInfoService iLTenantInfoService;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iLTenantInfoService, LTenantInfo.class, dataCenter));
	}
	
	/**
	 * 根据账号查询求租人信息
	 * @param keyId
	 * @param applyState
	 * @return
	 */
	@GetMapping(value = "/getTenantInfo")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> getTenantInfo(@RequestParam(name="accountNo",required=true) String accountNo) {
		LTenantInfo tenantInfo = iLTenantInfoService.queryTenantInfoByAccountNo(accountNo);
		if(tenantInfo==null) {
			return Result.error("此求租人信息不存在");
		}
		return Result.ok(tenantInfo);
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody LTenantInfo tenantInfo) {
		if(tenantInfo==null) {
			return Result.error("求租人息为空");
		}
		if(StringUtils.isEmpty(tenantInfo.getKeyId())) {
			if(LeaseConstants.求租人类型_个人.getCode().equals(tenantInfo.getTenantType())) {
				Result<?> r = uniqueCheckTools.isUniqueSingle(iLTenantInfoService, LTenantInfo.class, new String[] {"tenantCardCode"}, "keyId", tenantInfo, null, false);
				if(r==null || !r.isSuccess()) {
					return Result.error("身份证号不能重复");
				}
			}else if(LeaseConstants.求租人类型_企业.getCode().equals(tenantInfo.getTenantType())) {
				Result<?> r = uniqueCheckTools.isUniqueSingle(iLTenantInfoService, LTenantInfo.class, new String[] {"tenantUscc"}, "keyId", tenantInfo, null, false);
				if(r==null || !r.isSuccess()) {
					return Result.error("营业执照号或统一社会信用代码不能重复");
				}
			}else {
				return Result.error("求租人类型不正确");
			}
			
			iLTenantInfoService.save(tenantInfo);
			
		}else {
			if(LeaseConstants.求租人类型_个人.getCode().equals(tenantInfo.getTenantType())) {
				Result<?> r = uniqueCheckTools.isUniqueSingle(iLTenantInfoService, LTenantInfo.class, new String[] {"tenantCardCode"}, "keyId", null, tenantInfo, false);
				if(r==null || !r.isSuccess()) {
					return Result.error("身份证号不能重复");
				}
			}else if(LeaseConstants.求租人类型_企业.getCode().equals(tenantInfo.getTenantType())) {
				Result<?> r = uniqueCheckTools.isUniqueSingle(iLTenantInfoService, LTenantInfo.class, new String[] {"tenantUscc"}, "keyId", null, tenantInfo, false);
				if(r==null || !r.isSuccess()) {
					return Result.error("营业执照号或统一社会信用代码不能重复");
				}
			}else {
				return Result.error("求租人类型不正确");
			}
			iLTenantInfoService.updateById(tenantInfo);
		}
		
		return Result.ok();
	}
}

